home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Edition 10 / FreelogHS10.iso / Buzz / Buzz_Advanced_Pack.exe / {app} / Dev / Distortion / Distortion.cpp next >
C/C++ Source or Header  |  2001-08-27  |  5KB  |  263 lines

  1.  
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include <math.h>
  6. #include "../../MachineInterface.h"
  7.  
  8. CMachineParameter const paraThreshold = 
  9.     pt_word,                                        // type
  10.     "+threshold",
  11.     "Positive threshold level",                        // description
  12.     0,                                                // MinValue    
  13.     0xfffe,                                            // MaxValue
  14.     0xffff,                                            // NoValue
  15.     MPF_STATE,                                        // Flags
  16.     0x200,
  17. };
  18.  
  19. CMachineParameter const paraClamp = 
  20.     pt_word,                                        // type
  21.     "+clamp",
  22.     "Positive clamp level",                            // description
  23.     0,                                                // MinValue    
  24.     0xfffe,                                            // MaxValue
  25.     0xffff,                                            // NoValue
  26.     MPF_STATE,                                        // Flags
  27.     0x1000
  28. };
  29.  
  30. CMachineParameter const paraNegThreshold = 
  31.     pt_word,                                        // type
  32.     "-threshold",
  33.     "Negative threshold level",                        // description
  34.     0,                                                // MinValue    
  35.     0xfffe,                                            // MaxValue
  36.     0xffff,                                            // NoValue
  37.     MPF_STATE,                                        // Flags
  38.     0x200
  39. };
  40.  
  41. CMachineParameter const paraNegClamp = 
  42.     pt_word,                                        // type
  43.     "-clamp",
  44.     "Negative clamp level",                            // description
  45.     0,                                                // MinValue    
  46.     0xfffe,                                            // MaxValue
  47.     0xffff,                                            // NoValue
  48.     MPF_STATE,                                        // Flags
  49.     0x1000
  50. };
  51.  
  52. CMachineParameter const paraAmount = 
  53.     pt_byte,                                        // type
  54.     "Amount",
  55.     "Amount",                                        // description
  56.     0,                                                // MinValue    
  57.     0x7f,                                              // MaxValue
  58.     0xff,                                           // NoValue
  59.     MPF_STATE,                                        // Flags
  60.     0x7f
  61. };
  62.  
  63. CMachineParameter const *pParameters[] = 
  64.     // global
  65.     ¶Threshold,
  66.     ¶Clamp,
  67.     ¶NegThreshold,
  68.     ¶NegClamp,
  69.     ¶Amount
  70.     
  71. };
  72.  
  73. CMachineAttribute const attrSymmetric = 
  74. {
  75.     "Symmetric",
  76.     0,
  77.     1,
  78.     0
  79. };
  80.  
  81. CMachineAttribute const *pAttributes[] = 
  82. {
  83.     &attrSymmetric
  84. };
  85.  
  86.  
  87. #pragma pack(1)        
  88.  
  89. class gvals
  90. {
  91. public:
  92.     word threshold;
  93.     word clamp;
  94.     word negthreshold;
  95.     word negclamp;
  96.     byte amount;
  97. };
  98.  
  99. class avals
  100. {
  101. public:
  102.     int symmetric;
  103. };
  104.  
  105. #pragma pack()
  106.  
  107. CMachineInfo const MacInfo = 
  108. {
  109.     MT_EFFECT,                                // type
  110.     MI_VERSION,    
  111.     0,                                        // flags
  112.     0,                                        // min tracks
  113.     0,                                        // max tracks
  114.     5,                                        // numGlobalParameters
  115.     0,                                        // numTrackParameters
  116.     pParameters,
  117.     1,
  118.     pAttributes,
  119. #ifdef _DEBUG
  120.     "Jeskola Distortion (Debug build)",        // name
  121. #else
  122.     "Jeskola Distortion",                    // name
  123. #endif
  124.     "Dist",                                    // short name
  125.     "Oskari Tammelin",                        // author
  126.     NULL
  127. };
  128.  
  129.  
  130. class mi : public CMachineInterface
  131. {
  132. public:
  133.     mi();
  134.     virtual ~mi();
  135.  
  136.     virtual void Init(CMachineDataInput * const pi);
  137.     virtual void Tick();
  138.     virtual bool Work(float *psamples, int numsamples, int const mode);
  139.  
  140. private:
  141.             
  142.     
  143.  
  144. private:
  145.     float Threshold;
  146.     float Clamp;
  147.     float NegThreshold;
  148.     float NegClamp;
  149.     float Amount;
  150.     
  151.     gvals gval;
  152.     avals aval;
  153.  
  154. };
  155.  
  156. DLL_EXPORTS
  157.  
  158. mi::mi()
  159. {
  160.     GlobalVals = &gval;
  161.     AttrVals = (int *)&aval;
  162. }
  163.  
  164. mi::~mi()
  165. {
  166. }
  167.  
  168. void mi::Init(CMachineDataInput * const pi)
  169. {
  170.     Threshold = 65536 * 16.0;
  171.     Clamp = 65536 * 16.0;
  172.     NegThreshold = -65536 * 16.0;
  173.     NegClamp = -65536 * 16.0;
  174.     Amount = 1.0;
  175. }
  176.  
  177. void mi::Tick()
  178. {
  179.     if (gval.threshold != paraThreshold.NoValue)
  180.         Threshold = (float)(gval.threshold * 16.0);
  181.  
  182.     if (gval.clamp != paraClamp.NoValue)
  183.         Clamp = (float)(gval.clamp * 16.0);
  184.  
  185.     if (gval.negthreshold != paraNegThreshold.NoValue)
  186.         NegThreshold = (float)(gval.negthreshold * -16.0);
  187.  
  188.     if (gval.negclamp != paraNegClamp.NoValue)
  189.         NegClamp = (float)(gval.negclamp * -16.0);
  190.  
  191.     if (gval.amount != paraAmount.NoValue)
  192.         Amount = (float)(gval.amount * (1.0 / 0x7f));
  193. }
  194.  
  195.  
  196. bool mi::Work(float *psamples, int numsamples, int const mode)
  197. {
  198.     if (mode == WM_WRITE || mode == WM_NOIO)
  199.         return false;
  200.     
  201.     if (mode == WM_READ || Amount == 0)
  202.         return true;
  203.     
  204.     double const drymix = 1.0 - Amount;
  205.     float const clamp = (float)(Amount * Clamp);
  206.     double const threshold = Threshold;
  207.     float negclamp;
  208.     double negthreshold; 
  209.  
  210.     if (aval.symmetric)
  211.     {
  212.         negthreshold = -threshold;
  213.         negclamp = -clamp;
  214.     }
  215.     else
  216.     {
  217.         negthreshold = NegThreshold;
  218.         negclamp = (float)(Amount * NegClamp);
  219.     }
  220.  
  221.  
  222.     if (drymix < 0.001)
  223.     {
  224.         do 
  225.         {
  226.             double const s = *psamples;
  227.  
  228.             if (s >= threshold)
  229.                 *psamples = clamp;
  230.             else if (s <= negthreshold)
  231.                 *psamples = negclamp;
  232.  
  233.             psamples++;
  234.             
  235.         } while(--numsamples);
  236.     }
  237.     else
  238.     {
  239.         do 
  240.         {
  241.             double const s = *psamples;
  242.  
  243.             if (s >= threshold)
  244.                 *psamples = (float)(s * drymix + clamp);
  245.             else if (s <= negthreshold)
  246.                 *psamples = (float)(s * drymix + negclamp);
  247.  
  248.             psamples++;
  249.             
  250.         } while(--numsamples);
  251.     }
  252.  
  253.     return true;
  254. }
  255.  
  256.  
  257.